package net.biancheng.service.impl;

import java.util.ArrayList;
import java.util.List;

import net.biancheng.bean.SysUserRole;
import net.biancheng.common.constant.CommonConstant;
import net.biancheng.common.enums.SysDataStatus;
import net.biancheng.common.utils.DateUtils;
import net.biancheng.common.utils.SecurityUtils;
import net.biancheng.mapper.SysUserMapper;
import net.biancheng.mapper.SysUserRoleMapper;
import net.biancheng.service.ISysUserService;
import net.biancheng.bean.SysUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 系统用户Service业务层处理
 * 
 * @author lb
 * @date 2023-08-03
 */
@Service
public class SysUserServiceImpl implements ISysUserService
{
    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    /**
     * 查询系统用户
     * 
     * @param id 系统用户ID
     * @return 系统用户
     */
    @Override
    public SysUser selectSysUserById(Long id)
    {
        SysUser sysUser = sysUserMapper.selectSysUserById(id);
        if (sysUser != null) {
            //查询用户分配的角色
            List<Long> roleIdList = sysUserRoleMapper.selectRoleIdListByUserId(id);
            if (roleIdList != null && roleIdList.size() > 0) {
                sysUser.setRoleIds(roleIdList.toArray(new Long[roleIdList.size()]));
            }
        }
        return sysUser;
    }

    /**
     * 通过用户名查询用户
     *
     * @param userName 用户名
     * @return 用户对象信息
     */
    @Override
    public SysUser selectSysUserByUserName(String userName)
    {
        return sysUserMapper.selectSysUserByUserName(userName);
    }

    /**
     * 校验用户名称是否唯一
     *
     * @param sysUser 用户信息
     * @return 结果
     */
    @Override
    public String checkUserNameUnique(SysUser sysUser){
        Long userId = sysUser.getId() == null ? -1L : sysUser.getId();
        SysUser user = sysUserMapper.selectSysUserByUserName(sysUser.getUserName());
        if (user != null && user.getId().longValue() != userId.longValue()){ //不唯一，排除修改的情况
            return CommonConstant.NOT_UNIQUE;
        }
        return CommonConstant.UNIQUE;
    }

    /**
     * 校验手机号是否唯一
     *
     * @param sysUser 用户信息
     * @return 结果
     */
    @Override
    public String checkPhoneUnique(SysUser sysUser){
        Long userId = sysUser.getId() == null ? -1L : sysUser.getId();
        SysUser user = sysUserMapper.selectSysUserByPhone(sysUser.getPhone());
        if (user != null && user.getId().longValue() != userId.longValue()){//不唯一，排除修改的情况
            return CommonConstant.NOT_UNIQUE;
        }
        return CommonConstant.UNIQUE;
    }

    /**
     * 查询系统用户列表
     * 
     * @param sysUser 系统用户
     * @return 系统用户
     */
    @Override
    public List<SysUser> selectSysUserList(SysUser sysUser)
    {
        return sysUserMapper.selectSysUserList(sysUser);
    }

    /**
     * 新增系统用户
     * 
     * @param sysUser 系统用户
     * @return 结果
     */
    @Override
    public int insertSysUser(SysUser sysUser)
    {
        //用户注册时无法获取登录用户，默认取注册用户名
        if(StringUtils.isNotEmpty(sysUser.getCreateBy())){
            sysUser.setCreateBy(sysUser.getUserName());
        }
        sysUser.setCreateTime(DateUtils.getNowDate());
        sysUser.setStatus(SysDataStatus.OK.getCode());
        int rows = sysUserMapper.insertSysUser(sysUser);
        if (rows > 0) {
            // 新增用户与角色关联信息
            insertUserRole(sysUser);
        }
        return rows;
    }

    /**
     * 修改系统用户
     * 
     * @param sysUser 系统用户
     * @return 结果
     */
    @Override
    public int updateSysUser(SysUser sysUser)
    {
        sysUser.setUpdateTime(DateUtils.getNowDate());
        sysUser.setUpdateBy(SecurityUtils.getUsername());
        int rows = sysUserMapper.updateSysUser(sysUser);
        if (rows > 0) {
            // 先删除用户与角色关联信息
            sysUserRoleMapper.deleteSysUserRoleByUserId(sysUser.getId());
            // 新增用户与角色关联信息
            insertUserRole(sysUser);
        }
        return rows;
    }

    /**
     * 新增用户角色信息
     *
     * @param sysUser 用户
     */
    public void insertUserRole(SysUser sysUser)
    {
        Long userId = sysUser.getId();
        Long[] roleIds = sysUser.getRoleIds();
        if (roleIds != null && roleIds.length > 0)
        {
            // 新增用户与角色管理
            List<SysUserRole> list = new ArrayList<SysUserRole>();
            for (Long roleId : roleIds)
            {
                SysUserRole ur = new SysUserRole();
                ur.setUserId(userId);
                ur.setRoleId(roleId);
                list.add(ur);
            }
            sysUserRoleMapper.batchInsertSysUserRole(list);
        }
    }

    /**
     * 删除系统用户对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteSysUserByIds(Long[] ids)
    {
        return sysUserMapper.deleteSysUserByIds(ids);
    }

    /**
     * 删除系统用户信息
     * 
     * @param id 系统用户ID
     * @return 结果
     */
    @Override
    public int deleteSysUserById(Long id)
    {
        return sysUserMapper.deleteSysUserById(id);
    }
}